package org.game.db.vo;

import java.util.List;
import java.util.Map;

import org.cfg.EquipmentCfg;
import org.cfg.cache.EquipmentCfgCache;
import org.constant.smallkey.GoodInfoMapSmallKey;
import org.game.db.bean.Item;
import org.game.db.bean.Player;

import com.google.common.collect.Maps;
import com.sojoys.artifact.factory.json.FastJsonFactory;
import com.sojoys.artifact.tools.ToolMap;

/**
 * 灵装VO
 */
@SuppressWarnings("unchecked")
public class EquipmentVO extends Item {
    /**
     * 未装备时位置为0
     */
    public static final int NO_EQUIP_POSITION = 0;
    /**
     * 临时特殊属性Map
     */
    private static String TEMP_SPECIAL_ATTR = "tsa";
    /**
     * 临时特殊属性：老属性下标
     */
    public static String OLD_SPECIAL_ATTR_INDEX = "os_ai";
    /**
     * 临时特殊属性：新属性数组
     */
    public static String NEW_SPECIAL_ATTR_ARR = "ns_aa";

    public static int SPECIAL_ATTR_INDEX_TYPE = 0;
    public static int SPECIAL_ATTR_INDEX_VALUE = 1;
    public static int SPECIAL_ATTR_INDEX_CFG_ID = 2;


    private EquipmentCfg cfg;


    public EquipmentVO(long id, int num, int cid, Map infoMap, int playerId) {
        super(id, num, cid, infoMap, playerId);
        setExp(0);
        setLevel(1);
        setPosition(NO_EQUIP_POSITION);
    }

    public int getLevel() {
        return (int) getInfo().get(GoodInfoMapSmallKey.LEVEL);
    }

    public void setLevel(int level) {
        getInfo().put(GoodInfoMapSmallKey.LEVEL, level);
    }

    public int getExp() {
        return (int) getInfo().get(GoodInfoMapSmallKey.EXP);
    }

    public void setExp(int exp) {
        getInfo().put(GoodInfoMapSmallKey.EXP, exp);
    }
    
    public void addExp(int exp) {
    	int oldExp = (int) getInfo().computeIfAbsent(GoodInfoMapSmallKey.EXP, k->0);
        getInfo().put(GoodInfoMapSmallKey.EXP, exp+oldExp);
    }

    public int getPosition() {
        return (int) getInfo().get(GoodInfoMapSmallKey.POSITION);
    }

    public void setPosition(int position) {
        getInfo().put(GoodInfoMapSmallKey.POSITION, position);
    }

    public long getHeroId() {
        return ToolMap.getLong(GoodInfoMapSmallKey.HERO_ID, getInfo());
    }

    public void setHeroId(long heroId) {
        getInfo().put(GoodInfoMapSmallKey.HERO_ID, heroId);
    }

    /**
     * 卸下
     */
    public void takeOff() {
        setPosition(NO_EQUIP_POSITION);
        setHeroId(0);
    }

    /**
     * 装备
     */
    public void equip(long heroId, int position) {
        setPosition(position);
        setHeroId(heroId);
    }

    public List<List<Integer>> getSpecialAttrList() {
        return (List<List<Integer>>) getInfo().get(GoodInfoMapSmallKey.SPECIAL_ATTR);
    }

    public void setSpecialAttrList(List<List<Integer>> attrList) {
        getInfo().put(GoodInfoMapSmallKey.SPECIAL_ATTR, attrList);
    }
    /**
     * 临时特殊属性（待替换的特殊属性）
     *
     * @param oldAttrIndex 旧属性下标
     * @param newAttrInfo  新属性
     */
    public void setTempSpecialAttr(int oldAttrIndex, List<Integer> newAttrInfo) {
        Map map = Maps.newHashMap();
        map.put(OLD_SPECIAL_ATTR_INDEX, oldAttrIndex);
        map.put(NEW_SPECIAL_ATTR_ARR, newAttrInfo);
        getInfo().put(TEMP_SPECIAL_ATTR, map);
    }

    /**
     * 获取临时特殊属性
     */
    public Map getTempSpecialAttr() {
        return (Map) getInfo().get(TEMP_SPECIAL_ATTR);
    }

    /**
     * 替换特殊属性
     */
    public void changeAttr() {
        List<List<Integer>> specialAttrList = getSpecialAttrList();
        Map tempSpecialAttr = getTempSpecialAttr();
        if (tempSpecialAttr == null)
            return;
        int index = (int) tempSpecialAttr.get(OLD_SPECIAL_ATTR_INDEX);
        specialAttrList.remove(index);
        specialAttrList.add(index, (List<Integer>) tempSpecialAttr.get(NEW_SPECIAL_ATTR_ARR));
    }

    public void removeTempAttr() {
        getInfo().remove(TEMP_SPECIAL_ATTR);
    }

    public EquipmentCfg getCfg() {
        if (cfg == null) {
            cfg = EquipmentCfgCache.me().getById(getCid());
        }
        return cfg;
    }

    
}
